package com.xcity.game.achievement.helper;

import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;

/**
 * 成就类型：建造建筑群<br/>
 * 多个参数：（完成该条件所需建造的所有建筑id）<br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 * ①建筑1id ②建筑2id ③建筑3id …… n.建筑nid
 * @author yang.li
 *
 */
public class CreateBuildingsHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		if (refer.getState() == State.REWARDED) {
			return false;
		}
		int id = obtainBuildingId(vals);
		refer.getValue().put(String.valueOf(id), 1);
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() != State.INITED) {
			return false;
		}
		long[] buildings = refer.getTemplate().getConditions();
		for (int i = buildings.length - 1; i >= 0; i--) {
			int v = refer.getValue().getIntValue(String.valueOf(buildings[i]));
			if (v != 1) { // 未完成
				return false;
			}
		}
		refer.setState(State.FINISHED);
		return true;
	}
	
	static int obtainBuildingId(int... vals) {
		return vals != null && vals.length > 0 ? vals[0] : 0;
	}
	
}
